<template>
  <div class="w">
    <div class="image">
      <img src="../../../public/1775.jpg" alt="">
    </div>
    <van-form @submit="onSubmit" v-if="falg === 0">
      <van-field
    v-model="username"
    name="手机号"
    label="手机号"
    placeholder="手机号"
    autocomplete="off"
    :rules="[{ required: true, message: '请填手机号' }]"
        />
      <van-field
        v-model="sms"
        center
        clearable
        label="验证码"
        placeholder="请输入验证码"
        autocomplete="new-password"
      >
      <template #button>
        <van-button
        size="small"
        type="default"
        color="#ececec"
        round
        @click="SendSms"
        ref="sms"
        >发送验证码</van-button>
      </template>
      </van-field>
      <div class="van-ellipsis" @click="falg = 1">账号密码登录</div>
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" color="#ff4c4c">提交</van-button>
      </div>
    </van-form>
    <van-form @submit="onSubmit" v-else>
      <van-field
    v-model="username"
    name="账号"
    label="账号"
    placeholder="账号"
    autocomplete="off"
    :rules="[{ required: true,
       message: '请填正确格式账号',
       pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, }]"
        />
      <van-field
        v-model="password"
        type="password"
        label="密码"
        placeholder="请输入密码"
        :rules="[{
         required: true,
         message: '密码必须由数字、字母两种字符组成，长度在8-15位之间',
         pattern:/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,15}$/}]"
        autocomplete="new-password"
      >
      </van-field>
      <div class="van-ellipsis" @click="falg = 0">手机号登录</div>
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" color="#ff4c4c">提交</van-button>
      </div>
    </van-form>
    <p>
      <span @click="Jump('reg')">注册账号</span>
      <span>找回密码</span>
    </p>
    <van-checkbox v-model="checked" shape="square" icon-size="10px">
    我已阅读并同意
    <span>《用户使用协议》，《隐私权条款》</span>
    </van-checkbox>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { Login } from '@/api/User/index'
export default {
  data () {
    return {
      falg: 1,
      username: '',
      sms: '',
      password: '',
      checked: ''
    }
  },
  methods: {
    async onSubmit () {
      if (!this.checked) return Toast('请勾选用户使用协议')
      const res = {
        phone: this.username,
        pwd: this.password
      }
      if (this.falg === 1) {
        const data = await Login(res)
        this.$store.commit('log', data)
        if (data.success) {
          this.$router.replace('/')
        }
      } else {
        Toast('使用账号密码登录')
      }
    },
    Jump (url) {
      this.$router.push({ path: url })
    }
  }
}
</script>

<style lang="less" scoped>
.w{
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0px;
  bottom: 0px;
  background-color: #f7f7f7;
  display: flex;
  flex-direction: column;
  align-items: center;
  .image{
    width: 100%;
    height: 430px;
    background-color: antiquewhite;
    img{
      width: 100%;
      height: 100%;
    }
  }
  .van-cell{
    margin: 0 auto;
    width: 80%;
    border-radius: 40px;
    margin-bottom: 40px;
    background-color: #ececec;
  }
  .van-ellipsis{
    width: 200px;
    margin-left: 90px;
    margin-bottom: 100px;
    font-size: 23px;
    color: #33ccfe;
  }
  >p{
    width: 80%;
    margin: 20px auto;
    margin-bottom: 145px;
    display: flex;
    justify-content: space-around;
    >span{
      color: #666666;
      font-size: 23px;
    }
  }
  .van-checkbox{
    color: #666666;
    font-size: 23px;
    >span{
      font-size: 23px;
    color: #33ccfe;
    }
  }
}
</style>
